package com.wsp.service.service;


import com.wsp.apimanagement.pojo.dto.Result;
import com.wsp.apimanagement.pojo.entity.Source;
import com.wsp.apimanagement.service.SourceService;
import com.wsp.apimanagement.util.CommonResultBuilderUtil;
import com.wsp.service.mapper.SourceMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Random;

/**
 * Package: com.wsp.service.service
 * Description： TODO
 *
 * @Author: 巫思平
 * Copyright: Copyright (c) 2020
 * Version: 1
 */
@Service
public class SourceServiceImpl implements SourceService {
    @Autowired
    private SourceMapper sourceMapper;
    @Override
    public Result<List<Source>> getRandomSource(int num) {
        Integer max = sourceMapper.getMaxSourceId();
        final int i = new Random().nextInt(max);
        return Result.<List<Source>>builder().code(200).msg("SUCCESS").data(sourceMapper.getRandomSource(num,i)).build();
    }

    @Override
    public Result<List<Source>> getModuleSource(long moduleId) {
        final List<Source> source= sourceMapper.getSourceByModuleId(moduleId);
        if (source==null) {
            return CommonResultBuilderUtil.commonError("资源失效");
        }
        return Result.<List<Source>>builder().code(200).msg("SUCCESS").data(source).build();
    }
}
